/**
 * @(#)PDBizXiaoMi.java 1.0 2013/9/17
 * Copyright 2013 PD, Inc. All rights reserved.
 */
package cn.broadin.app.hotel.bz;

import cn.broadin.app.hotel.factory.ApplicationFactory;
import cn.broadin.app.hotel.stb.IPDAPK;
import cn.broadin.app.hotel.stb.IPDDataAccess;
import cn.broadin.app.hotel.stb.IPDMediaPlay;
import cn.broadin.app.hotel.stb.IPDSmartCard;
import cn.broadin.app.hotel.stb.IPDSysSetting;
import cn.broadin.app.hotel.stb.IPDVideoPlayer;
import cn.broadin.app.hotel.stb.impl.common.PDAPKCommon;
import cn.broadin.app.hotel.stb.impl.common.PDDataAccessCommon;
import cn.broadin.app.hotel.stb.impl.common.PDMediaPlayCommon;
import cn.broadin.app.hotel.stb.impl.common.PDSmartCardCommon;
import cn.broadin.app.hotel.stb.impl.common.PDSysSettingCommon;
import cn.broadin.app.hotel.stb.impl.common.PDVideoPlayerCommon;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebView;


/**
 * PDBizXiaoMi PD应用接口(小米)
 * @author fenghui
 *
 */
public class PDBizXiaoMi extends BasePDBiz{

	private static final String TAG = "PDBizXiaoMi";		//log tag
	private Activity context = null;							//activity
	
	
	public PDBizXiaoMi(Activity ctx, WebView wv) {
		super(ctx, wv);
		context = ctx;
	}

	@Override
	public IPDDataAccess getDataAccess() {
		Log.i(TAG, "getDataAccess() method run.");
		IPDDataAccess dataAccess = new PDDataAccessCommon(context);
		return dataAccess;
	}

	@Override
	public IPDMediaPlay getMediaPlay() {
		Log.i(TAG, "getMediaPlay() method run.");
		return new PDMediaPlayCommon(context);
	}

	@Override
	public IPDSmartCard getSmartCard() {
		Log.i(TAG, "getSmartCard() method run.");
		return new PDSmartCardCommon(context);
	}

	@Override
	public IPDSysSetting getSysSetting() {
		Log.i(TAG, "getSysSetting() method run.");
		IPDSysSetting pdSysSetting = new PDSysSettingCommon(context);
		pdSysSetting.setEnv("pd_ptIp", ApplicationFactory.getApplication().PT_IPPORT());
		pdSysSetting.setEnv("pd_proxyIp", ApplicationFactory.getApplication().PROXY_IPPORT());
		return pdSysSetting;
	}
	
	@Override
	public IPDAPK getAPK() {
		Log.i(TAG, "getAPK() method run.");
		IPDAPK pdAPK = new PDAPKCommon(context);
		return pdAPK;
	}
	
	@Override
	public IPDVideoPlayer getVideoPlaer() {
		Log.i(TAG, "getVideoPlaer() method run.");
		IPDVideoPlayer videoPlayer = new PDVideoPlayerCommon(context);
		return videoPlayer;
	}
	
	/**
	 * 处理遥控按键
	 * @param event
	 */
	@Override
	public void grabEvent(KeyEvent event) {
		super.grabEvent(event);
		if(event.getAction() == KeyEvent.ACTION_DOWN) {
			int keyCode = event.getKeyCode();
			Log.i(TAG, "grabEvent keycode is : " + keyCode);
			int code = 0;
			switch (keyCode) {
				case KeyEvent.KEYCODE_BACK:        //后退键
					if(webView.getUrl().equals(ApplicationFactory.getApplication().APP_DEFAULTURL(""))) {
						exitApp();
					} else if(webView.getUrl().equals(ApplicationFactory.getApplication().APP_ABOUTURL())) {
						webView.goBack();
					} else {
						code = 340;
					}
					break;
				case KeyEvent.KEYCODE_MENU:			//菜单键
					break;
				case KeyEvent.KEYCODE_VOLUME_UP:	//音量加
					code = 595;
					break;
				case KeyEvent.KEYCODE_VOLUME_DOWN:	//音量减
					code = 596;
					break;
				case 164:							//静音键
					code = 597;
					break;
			}
			if(code != 0) {
				Log.i(TAG, "javascript:grabEvent(" + code + ")");
				webView.loadUrl("javascript:grabEvent(" + code + ")");
			}
		}
	}
}